package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.dao.MChannelMapper;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Channel;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Dept;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ChannelServiceImpl implements IChannelService {

    @Resource
    MChannelMapper mapper;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public void deleteChannel(Integer deptId) {

        Channel channel = mapper.selectById(deptId);

        Wrapper<Channel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + channel.getId() + "]%");
        List<Channel> subChannels = mapper.selectList(wrapper);
        for (Channel temp : subChannels) {
            temp.deleteById();
        }

        channel.deleteById();
    }


    public List<Map<String, Object>> getChannel() {
        String sql = "select id, pids,channelname from channel where channelname!='请选择' order by  num asc;";
        List<Map<String, Object>> channelList = sqlMapper.selectList(sql);
        List<Map<String, Object>> oneChannelList = new ArrayList<>();
        List<Map<String, Object>> twoChannelList = new ArrayList<>();
        List<Map<String, Object>> thrChannelList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < channelList.size(); i++) {
            if (channelList.get(i).get("pids").toString().split(",").length < 2) {
                oneChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 2) {
                twoChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 3) {
                thrChannelList.add(channelList.get(i));
            }
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < oneChannelList.size(); i++) {
            Map<String, Object> oneChannelMap = new HashMap<String, Object>();
            List<Map<String, Object>> listmap2 = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < twoChannelList.size(); j++) {
                if (("[" + oneChannelList.get(i).get("id").toString() + "]").equals(twoChannelList.get(j).get("pids").toString().split(",")[1])) {
                    Map<String, Object> twoChannelMap = new HashMap<String, Object>();
                    List list3 = new ArrayList<>();
                    for (int k = 0; k < thrChannelList.size(); k++) {
                        if (("[" + twoChannelList.get(j).get("id").toString() + "]").equals(thrChannelList.get(k).get("pids").toString().split(",")[2])) {
                            list3.add(thrChannelList.get(k).get("channelname").toString());
                        }
                    }
                    twoChannelMap.put("name", twoChannelList.get(j).get("channelname").toString());
                    twoChannelMap.put("areaList", list3);
                    listmap2.add(twoChannelMap);
                    oneChannelMap.put("citiesList", listmap2);
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                }else{
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                    oneChannelMap.put("citiesList", listmap2);
                }
            }
            relist.add(oneChannelMap);
            if (relist.get(i).size() == 0) {
                relist.remove(relist.get(i));
            }
        }
        return relist;
    }

    public List<Map<String, Object>> getChannels() {
        String sql = "select id, pids,channelname from channel where channelname!='直接上门' and channelname!='老带新' and channelname!='请选择' order by  num asc;";
        List<Map<String, Object>> channelList = sqlMapper.selectList(sql);
        List<Map<String, Object>> oneChannelList = new ArrayList<>();
        List<Map<String, Object>> twoChannelList = new ArrayList<>();
        List<Map<String, Object>> thrChannelList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < channelList.size(); i++) {
            if (channelList.get(i).get("pids").toString().split(",").length < 2) {
                oneChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 2) {
                twoChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 3) {
                thrChannelList.add(channelList.get(i));
            }
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < oneChannelList.size(); i++) {
            Map<String, Object> oneChannelMap = new HashMap<String, Object>();
            List<Map<String, Object>> listmap2 = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < twoChannelList.size(); j++) {
                if (("[" + oneChannelList.get(i).get("id").toString() + "]").equals(twoChannelList.get(j).get("pids").toString().split(",")[1])) {
                    Map<String, Object> twoChannelMap = new HashMap<String, Object>();
                    List list3 = new ArrayList<>();
                    for (int k = 0; k < thrChannelList.size(); k++) {
                        if (("[" + twoChannelList.get(j).get("id").toString() + "]").equals(thrChannelList.get(k).get("pids").toString().split(",")[2])) {
                            list3.add(thrChannelList.get(k).get("channelname").toString());
                        }
                    }
                    twoChannelMap.put("name", twoChannelList.get(j).get("channelname").toString());
                    twoChannelMap.put("areaList", list3);
                    listmap2.add(twoChannelMap);
                    oneChannelMap.put("citiesList", listmap2);
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                }else{
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                    oneChannelMap.put("citiesList", listmap2);
                }
            }
            relist.add(oneChannelMap);
            if (relist.get(i).size() == 0) {
                relist.remove(relist.get(i));
            }
        }
        return relist;
    }

    public List<Map<String, Object>> getChannelsss() {
        String sql = "select id, pids,channelname from channel  order by  num asc;";
        List<Map<String, Object>> channelList = sqlMapper.selectList(sql);
        List<Map<String, Object>> oneChannelList = new ArrayList<>();
        List<Map<String, Object>> twoChannelList = new ArrayList<>();
        List<Map<String, Object>> thrChannelList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < channelList.size(); i++) {
            if (channelList.get(i).get("pids").toString().split(",").length < 2) {
                oneChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 2) {
                twoChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 3) {
                thrChannelList.add(channelList.get(i));
            }
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < oneChannelList.size(); i++) {
            Map<String, Object> oneChannelMap = new HashMap<String, Object>();
            List<Map<String, Object>> listmap2 = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < twoChannelList.size(); j++) {
                if (("[" + oneChannelList.get(i).get("id").toString() + "]").equals(twoChannelList.get(j).get("pids").toString().split(",")[1])) {
                    Map<String, Object> twoChannelMap = new HashMap<String, Object>();
                    List list3 = new ArrayList<>();
                    for (int k = 0; k < thrChannelList.size(); k++) {
                        if (("[" + twoChannelList.get(j).get("id").toString() + "]").equals(thrChannelList.get(k).get("pids").toString().split(",")[2])) {
                            list3.add(thrChannelList.get(k).get("channelname").toString());
                        }
                    }
                    twoChannelMap.put("name", twoChannelList.get(j).get("channelname").toString());
                    twoChannelMap.put("areaList", list3);
                    listmap2.add(twoChannelMap);
                    oneChannelMap.put("citiesList", listmap2);
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                }else{
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                    oneChannelMap.put("citiesList", listmap2);
                }
            }
            relist.add(oneChannelMap);
            if (relist.get(i).size() == 0) {
                relist.remove(relist.get(i));
            }
        }
        return relist;
    }

    public List<Map<String, Object>> getChannelss() {
        String sql = "select id, pids,channelname from channel where channelname!='直接上门' and channelname!='老带新' order by  num asc;";
        List<Map<String, Object>> channelList = sqlMapper.selectList(sql);
        List<Map<String, Object>> oneChannelList = new ArrayList<>();
        List<Map<String, Object>> twoChannelList = new ArrayList<>();
        List<Map<String, Object>> thrChannelList = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < channelList.size(); i++) {
            if (channelList.get(i).get("pids").toString().split(",").length < 2) {
                oneChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 2) {
                twoChannelList.add(channelList.get(i));
            } else if (channelList.get(i).get("pids").toString().split(",").length == 3) {
                thrChannelList.add(channelList.get(i));
            }
        }
        List<Map<String, Object>> relist = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < oneChannelList.size(); i++) {
            Map<String, Object> oneChannelMap = new HashMap<String, Object>();
            List<Map<String, Object>> listmap2 = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < twoChannelList.size(); j++) {
                if (("[" + oneChannelList.get(i).get("id").toString() + "]").equals(twoChannelList.get(j).get("pids").toString().split(",")[1])) {
                    Map<String, Object> twoChannelMap = new HashMap<String, Object>();
                    List list3 = new ArrayList<>();
                    for (int k = 0; k < thrChannelList.size(); k++) {
                        if (("[" + twoChannelList.get(j).get("id").toString() + "]").equals(thrChannelList.get(k).get("pids").toString().split(",")[2])) {
                            list3.add(thrChannelList.get(k).get("channelname").toString());
                        }
                    }
                    twoChannelMap.put("name", twoChannelList.get(j).get("channelname").toString());
                    twoChannelMap.put("areaList", list3);
                    listmap2.add(twoChannelMap);
                    oneChannelMap.put("citiesList", listmap2);
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                }else{
                    oneChannelMap.put("name", oneChannelList.get(i).get("channelname").toString());
                    oneChannelMap.put("citiesList", listmap2);
                }
            }
            relist.add(oneChannelMap);
            if (relist.get(i).size() == 0) {
                relist.remove(relist.get(i));
            }
        }
        return relist;
    }

}
